package com.iemes.controller.qc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.UnqualifiedFormMap;
import com.iemes.mapper.UnqualifiedMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

//不合格品管理
@Controller
@RequestMapping("/un/")
public class UnqualifiedController extends BaseController {

    @Inject
    private UnqualifiedMapper unqualifiedMapper;


    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/qc/un/un_list";
    }

    //获取分页数据
    @ResponseBody
    @RequestMapping("getUnByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort, HttpServletRequest request) throws Exception {
        PageHelper.startPage(1, 10);

        //产品名称
//        String product = request.getParameter("item_bom_no");
//        if(product == null || product.equals(" ") || product.equals("null") || product.equals("")){
//            List<UnqualifiedFormMap> unqualifiedFormMapList = orderMapper.findOrderList();
//            PageInfo pageInfo = new PageInfo<>(unqualifiedFormMapList);
//            return pageInfo;
//        }else {
//            String orderFormMapList = orderMapper.findStockName(product);
//            if(orderFormMapList != null || orderFormMapList.equals(" ")){
//                List<OrderFormMap> orderListId = orderMapper.findOrderListId(orderFormMapList);
//                PageInfo pageInfo = new PageInfo<>(orderListId);
//                return pageInfo;
//            }
//            return null;
//        }
            List<UnqualifiedFormMap> unList = unqualifiedMapper.getAll();
            PageInfo pageInfo = new PageInfo<>(unList);
            return pageInfo;
    }

    //打开添加页面
    @RequestMapping("openAdd")
    public String openProcessManagementUI(){
        return Common.BACKGROUND_PATH + "/qc/un/un_add";
    }

    //增加数据
    //添加数据
    @ResponseBody
    @RequestMapping("addUn")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addOrder(){
        UnqualifiedFormMap unqualifiedFormMap = getFormMap(UnqualifiedFormMap.class);
        try {
            //根据成品id获取成品编码
            unqualifiedMapper.addEntity(unqualifiedFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    //打开编辑页面
    @RequestMapping("openEdit")
    public String openEditUi(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        model.addAttribute("un",unqualifiedMapper.selChose(id));
        return Common.BACKGROUND_PATH + "/qc/un/un_edit";
    }

    //保存修改数据
    @ResponseBody
    @RequestMapping("editUn")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editUn(String txtGroupsSelect) throws Exception {
        UnqualifiedFormMap unqualifiedFormMap = getFormMap(UnqualifiedFormMap.class);
        unqualifiedMapper.deleteByAttribute("id", unqualifiedFormMap.get("id")+"", UnqualifiedFormMap.class);
        unqualifiedMapper.addEntity(unqualifiedFormMap);
        return "success";
    }

}
